package com.esincrunch.tool;

import com.esincrunch.sms.ContactItem;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context; 
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.provider.ContactsContract.PhoneLookup;

public class SMSReceiver extends BroadcastReceiver {
	private static final String TAG = "SMSReceiver";
	SharedPreferences sp;
	@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
		
        Bundle bundle = intent.getExtras();
        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[messages.length];
        String mAddress;
        String mBody;
        Long mDate;
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        Boolean isOn = sp.getBoolean("isOn", true);
        Log.e(TAG,isOn.toString());
        if(isOn){
        	for (int n = 0; n < messages.length; n++) {
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
                mAddress=smsMessage[n].getDisplayOriginatingAddress().toString();
                mBody=smsMessage[n].getDisplayMessageBody().toString();
                mDate=smsMessage[n].getTimestampMillis();
                if(isInBlockList(mAddress,context)==true){
                	addBlackList(mAddress,mBody,mDate,context);
                    this.abortBroadcast();
                    Log.e(TAG,"abortBroadcast");
                    Toast.makeText(context,R.string.sr_block_tip, Toast.LENGTH_LONG).show();  
                    intent=new Intent(context,SMSBlockMan.class);
                    //Log.e(TAG,intent.getAction());
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
                    context.startActivity(intent);
                    //Log.e(TAG,intent.getAction());
                    
                }
                if(smsMessage[n].getMessageBody().contains("spam words")){
                    this.abortBroadcast();
                    Log.e(TAG,"abortBroadcast");
                }
                
                Log.e(TAG,smsMessage[n].getOriginatingAddress()+" "+smsMessage[n].getMessageBody()+" "+smsMessage[n].getIndexOnIcc());
            }
        }
    }
	private Boolean  isInBlockList(String Address,Context context){
		BlockReader rd=new BlockReader();
		return rd.isInBlockList(Address, context);
		
	}
	private void addBlackList(String mAddress,String mBody,Long mDate,Context context){
    	ContentValues contentValues;
    	contentValues = new ContentValues();
    	Long now = Long.valueOf(System.currentTimeMillis());
    	ContentResolver cr=context.getContentResolver();
		contentValues.put(ListProvider.SERVICENO, mAddress);  
		contentValues.put(ListProvider.SENDTEXT, mBody);
		contentValues.put(ListProvider.SENDTIME, mDate);
		contentValues.put(ListProvider.CRDAT, now);
		contentValues.put(ListProvider.TYPENO, "0");
		
		Cursor cur=cr.query(ListProvider.CONTENT_URI,new String[] {ListProvider.SENDTEXT}, "sendtime="+mDate, null, ListProvider.SENDTIME);
		if(cur != null &&
				cur.moveToFirst()) {
				int idx = cur.getColumnIndex(ListProvider.SENDTEXT);
				mBody = cur.getString(idx)+mBody;
				ContentValues cv=new ContentValues();
				cv.put(ListProvider.SENDTEXT, mBody);
				cr.update(ListProvider.CONTENT_URI, cv, "sendtime="+mDate, null);	
		}
		else{
			cr.insert(ListProvider.CONTENT_URI, contentValues);	
		}
		
		
    }


}
